home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1 / Ian and Stuart's One (Australia).iso / Australasian Legends / Commercial / Rainbow Hill / MacDOS™ 2.0.0 / User's Guide / 08 Batches and Programs < prev    next >
Text File  |  1994-09-20  |  12KB  |  279 lines

  1. 8  Batches and Programs
  2.        MacDOS recognises four types of "executable" files:
  3.        •  Applications: Files of type 'APPL'.
  4.        •  Compiled AppleScripts: Files of type 'osas'.
  5.        •  Text AppleScripts, of type 'TEXT' and creator 'ToyS'.
  6.        •  Batch   programs,   of  type  'TEXT'  containing   MacDOS
  7.           commands  to be interpreted and executed one  line  at  a
  8.           time.
  9.  
  10.        You  have  two  ways  of  telling  MacDOS  what  program  to
  11.        execute:
  12.          •    by  writing  its file specification at the  beginning
  13.           of a command line;
  14.          •   by executing the command  call fileSpec .
  15.        The  two  strategies are only different if you  are  already
  16.        executing  in  batch: in the first case, control  is  simply
  17.        transferred to the new program; with CALL, execution of  the
  18.        current  batch  resumes  after  the  application  has   been
  19.        launched or the new batch program has terminated.
  20.  
  21.        Instead of having to type full file specifications, you  can
  22.        take  advantage of the system variable PATH  and  just  type
  23.        the  name  of the application or batch program. If you  keep
  24.        all applications and batch programs (or their aliases) in  a
  25.        small number of folders, you will never have to type a  full
  26.        file specification at all.
  27.  
  28.        Note  that  MacDOS,  unlike DOS, does not automatically  add
  29.        the  extension  .EXE  when  looking  for  executable  files.
  30.        Neverthess, similarly to DOS, MacDOS automatically adds  the
  31.        extension .BAT when looking for batch programs.
  32.  
  33. Introduction to Batch Programs
  34.        After  opening  a batch file for reading, MacDOS  reads  and
  35.        attempts  to  execute one line of text at  a  time.  In  its
  36.        simplest  form, a batch program is therefore just a  way  of
  37.        storing a series of MacDOS commands so that you do not  need
  38.        to type them over and over again.
  39.  
  40.        Nevertheless,  there  are a series of  features  which  make
  41.        batch programs very useful:
  42.          •    Several  MacDOS  commands are only  available  within
  43.           batch programs.
  44.          •    You  can  control  the execution  of  batch  programs
  45.           through launch parameters.
  46.          •    You  can setup a batch program in such a way that  it
  47.           behaves like a Macintosh application.
  48.          •    You  can  document your algorithms and the  reasoning
  49.           behind them by adding lines of comments.
  50.  
  51.        To  add lines of comments, you can use the command REM  (for
  52.        REMark).  REM tells MacDOS to ignore the rest  of  the  line
  53.        and  continue  with  the  next one.  You  can  also  use  an
  54.        exclamation mark ('!') in the same way.
  55.  
  56.        If  you  redirect  the  output of a  batch  program,  MacDOS
  57.        automatically  applies the redirection  to  each  individual
  58.        command  in  the batch file, including nested batches.  Note
  59.        that the output of batch programs cannot be piped.
  60.  
  61. Jumps and Labels
  62.        Normally,  MacDOS executes the lines of a batch  program  in
  63.        sequence, as they appear in the file. You can direct  MacDOS
  64.        to  interrupt  the  sequence and resume execution  somewhere
  65.        else in the file by using the command GOTO label.
  66.  
  67.        Almost any word or number can be used as a label but, to  be
  68.        recognised  and  accepted  as  such,  it  must  satisfy  the
  69.        following conditions:
  70.          •   It cannot contain spaces or tabs.
  71.          •   It cannot begin with a percent sign.
  72.          •    It  must  be  the first word in a line  which  begins
  73.           with a colon.
  74.          •   It must be unique within the file.
  75.  
  76.        Before  executing a batch program, MacDOS  scans  the  whole
  77.        file  looking for lines which begin with a colon.  For  each
  78.        one  of such lines, MacDOS builds a "label entry" containing
  79.        the  first word after the colon and the file offset  of  the
  80.        next  line.  Note that MacDOS ignores whatever  follows  the
  81.        label,  so  that  you  can use the  rest  of  the  line  for
  82.        commenting.  Also note that the colon identifies  the  label
  83.        but is not part of it.
  84.  
  85.        When  MacDOS  encounters a GOTO during batch  execution,  it
  86.        scans the label entries looking for the requested label.  If
  87.        it  finds an entry with the matching label, it then  resumes
  88.        execution of the batch program from the line with  the  file
  89.        offset found in the entry.
  90.  
  91. Replaceable Parameters
  92.        Whether  you  just type a batch filename or use the  command
  93.        CALL,  you  can  pass  to  the batch  program  a  series  of
  94.        parameters.
  95.  
  96.        The  parameter %0 stores the name of the batch file  itself,
  97.        while  the  parameters  %1 to %9 are available  for  you  to
  98.        define as you need.
  99.  
  100.        Each  one  of  these replaceable parameters  consists  of  a
  101.        string  and  is  accessible from within  the  batch  program
  102.        through  a  number preceded by a percent sign. For  example,
  103.        if you type:
  104.           myBatch name "a full address"
  105.        the  program   myBatch  will be able to  access  the  string
  106.        name   as  %1 and the string  a full address  as  %2  .  The
  107.        string  myBatch  will be available as %0 .
  108.  
  109.        Despite  the limitation in the number of parameters,  MacDOS
  110.        lets  you pass to a batch program up to 24 strings. You  can
  111.        access  the  additional  15 strings  by  using  the  command
  112.        SHIFT.  SHIFT copies %1 to %0, %2 to %1, ..., and %9 to  %8;
  113.        it  then stores the 10th string into %9. By "pushing in" one
  114.        new  string  at  a time in this way, you can  acess  all  15
  115.        additional strings.
  116.  
  117. Conditional Statements
  118.        MacDOS  lets you implement conditional statements  with  the
  119.        command  IF.  The general format of IF is  if  condition  do
  120.        aCommand  , where  aCommand  is executed only if   condition
  121.        is true.
  122.  
  123.        Valid conditions are:
  124.           string1 == string2
  125.           exist fileSpec
  126.           existdir folderSpec
  127.           not existdir folderSpec
  128.  
  129.        As you can see from the last example, you can also invert  a
  130.        condition  by  inserting  a 'not' between  the  IF  and  the
  131.        condition.
  132.  
  133. Loops
  134.        Very  often, a particular sequence of commands needs  to  be
  135.        repeated  for  each  file belonging to a particular  set  or
  136.        until  certain  special conditions are  verified.  To  serve
  137.        this purpose, MacDOS provides the command: FOR.
  138.  
  139.        FOR  lets you define a set of files and then operate on each
  140.        one  of  them. In its simplest form, FOR executes  a  single
  141.        command included in the same line:
  142.           for %name in (set) do aCommand
  143.        where  set is a comma-separated list of file specifications,
  144.        and  aCommand  can  refer to the current  file  through  the
  145.        special variable %name.
  146.  
  147.        To  execute several commands for each file, you can  use  an
  148.        extended format of FOR:
  149.           for %name in (set) do begin
  150.               ...  several commands which can refer to the  current
  151.        file through the
  152.                    global variable %name% ...
  153.              next name
  154.  
  155.        The  most  general  way  of implementing  loops  is  with  a
  156.        combination  of an IF and a GOTO. For example, if  you  need
  157.        to  repeat  certain operations until a certain condition  is
  158.        verified,  you can do as follows (equivalent a  WHILE  or  a
  159.        FOR in C or a WHILE-DO in Pascal):
  160.           set k = 1
  161.           :LOOP_LBL
  162.              if k == 5 goto BREAK_LBL
  163.              ... here you do what you need to do ...
  164.              incr k
  165.              goto LOOP_LBL
  166.           :BREAK_LBL
  167.  
  168.        Alternatively, you can place the IF at the end of  the  loop
  169.        (equivalent  to  a DO in C or Fortran or a  REPEAT-UNTIL  in
  170.        Pascal):
  171.           set control=initial
  172.           :LOOP_LBL
  173.              ... here you do what you need to do ...
  174.               ...  and  also set the control variable in  order  to
  175.        exit the loop ...
  176.              if control==done goto BREAK_LBL
  177.              goto LOOP_LBL
  178.           :BREAK_LBL
  179.  
  180. Error Handling
  181.        Beside  reporting  error  messages  in  clear,  MacDOS  also
  182.        stores  error  codes  into the global variable   doserr.  To
  183.        convert  the  error  codes to their corresponding  messages,
  184.        you can use the command SHOW.
  185.  
  186.        The  command  ONERROR  lets you trap all  the  errors  which
  187.        occur  while executing a batch program. This is particularly
  188.        useful  when  you  execute a batch program  with  ECHO  OFF,
  189.        because  in  that case MacDOS suppresses error  and  warning
  190.        messages.
  191.  
  192.        ONERROR  lets  you  specify the label of an  error  handling
  193.        procedure. For example,
  194.           onerror label
  195.           ... first command for which you want to trap errors ...
  196.            ...  second  command for which you want to  trap  errors
  197.        ...
  198.           ...
  199.           onerror
  200.        is  functionally equivalent to the following  less  readable
  201.        construct:
  202.           set doserr=0
  203.           ... first command for which you want to trap errors ...
  204.           if not %doserr% == 0 goto label
  205.            ...  second  command for which you want to  trap  errors
  206.        ...
  207.           if not %doserr% == 0 goto label
  208.           ...
  209.           set doserr=0
  210.  
  211. User Interface
  212.        While  executing  in  batch, MacDOS  normally  displays  all
  213.        commands  as they are executed. With the command  ECHO  OFF,
  214.        you  can direct MacDOS to suppress both the echoing  of  the
  215.        commands  and  their  output, including  warning  and  error
  216.        messages. You can then re-enable the display with ECHO ON.
  217.  
  218.        If  you  only  want  to  suppress the  output  of  a  single
  219.        command,  you can do so by attaching an '@' to the front  of
  220.        the command name.
  221.  
  222.        By  typing  as  first  line of a batch program  the  command
  223.        @echo off , you make the program completely silent.
  224.  
  225.        In  order  to  display variable values  and  other  messages
  226.        while  echoing is disabled, you can use ECHO as  illustrated
  227.        in the following examples:
  228.           echo Now processing file No %fileNum%
  229.           echo File %1 found: it is in folder %foldSpec%
  230.  
  231.        After  ECHOing  an intermediate result, you  might  need  to
  232.        suspend   execution  of  a  batch  program  and   wait   for
  233.        confirmation  before continuing. For this  purpose  you  can
  234.        use  the  command  PAUSE. When MacDOS encounters  PAUSE,  it
  235.        displays the message:
  236.           press any key to continue ...
  237.        and  waits  for your reply. If you type cmd-dot  or  cntl-C,
  238.        MacDOS then asks
  239.           Terminate batch job (Y/N) ?
  240.        You can then terminate the job by replying 'Y' or 'y'.
  241.  
  242. Batches that behave like Applications
  243.        Any  file  of type 'TEXT' is accepted by MacDOS as  a  valid
  244.        batch  program.  Nevertheless, if you want to  automatically
  245.        launch  MacDOS  by double clicking on the icon  of  a  batch
  246.        file, you need to set its creator to 'mDOS'.
  247.  
  248.        By  using  the  command  EXIT as last  line,  you  can  also
  249.        automatically terminate MacDOS and return to the  Finder  at
  250.        the end of the batch program.
  251.  
  252.        Here is a simple program that is actually used every day  to
  253.        back up this User's Guide while it is being written:
  254.           confirm off
  255.           copy "\MacDOS folder\product documents\manual" 2: /u/v
  256.           exit
  257.        It  copies  to  a floppy all files which have  been  updated
  258.        since the last backup.
  259.  
  260.        You  can  preset  to 'mDOS' the creator  of  text  files  by
  261.        setting the system variable CREATOR:
  262.           set creator=mDOS
  263.  
  264.        Alternatively,  you  can change to  'mDOS'  the  creator  of
  265.        existing files:
  266.           ren/c!mDOS fileSpec
  267.  
  268.        Note  that  MacDOS  executes the batch program  autoexec.bat
  269.        (see  below)  before executing the batch  program  that  you
  270.        have double clicked.
  271.  
  272.        If  you  keep  MacDOS  running all  the  times,  instead  of
  273.        aliasing  the  MacDOS application into the  folder  "Startup
  274.        Items",  you  might like to replace all the  items  in  that
  275.        folder  with  a  small batch program which CALLs  them  all.
  276.        They  would be launched in the order in which you call  them
  277.        (MacDOS will always be the first one, though).
  278.  
  279.